package cn.edu.demo.service;

import cn.edu.demo.mapper.PassengerMapper;
import cn.edu.demo.pojo.Orders;
import cn.edu.demo.pojo.Passenger;
import cn.edu.demo.pojo.PassengerExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PassengerServiceImpl implements PassengerService{

    @Autowired
    PassengerMapper mapper;

    @Override
    public Passenger getPassenger(String uid) {
        PassengerExample example = new PassengerExample();
        PassengerExample.Criteria criteria = example.or();
        criteria.andIdEqualTo(uid);
        return mapper.selectByExample(example).get(0);
    }

    @Override
    public Passenger login(Long phone, String password) {
        PassengerExample example = new PassengerExample();
        PassengerExample.Criteria criteria = example.or();
        criteria.andPhoneEqualTo(phone);
        criteria.andPasswordEqualTo(password);
        return mapper.selectByExample(example).size()!=0?mapper.selectByExample(example).get(0):null;
    }

    @Override
    public int enroll(String username,String password,Long phone,String id,Integer sex,String fullnameP){
        PassengerExample example = new PassengerExample();
        example.setOrderByClause("passenger_ID DESC");
        Passenger cache = mapper.selectByExample(example).get(0);
        Passenger passenger = new Passenger();
        passenger.setFullnameP(fullnameP);
        passenger.setPassword(password);
        passenger.setPhone(phone);
        passenger.setPassengerId(String.valueOf(Integer.valueOf(cache.getPassengerId())+1));
        passenger.setSex(sex);
        passenger.setUsername(username);
        return mapper.insertSelective(passenger);
    }
}
